//
//  Subject.m
//  test
//
//  Created by Christian Speich on 11.01.08.
//  Copyright 2008 Christian Speich. All rights reserved.
//

#import "Subject.h"
#import "Mark.h"
#import "Lesson.h"

@implementation Subject

- (id) init
{
	self = [super init];
	if (self != nil) {
		marks = [[NSMutableArray alloc] initWithCapacity:1];
		lessons = [[NSMutableArray alloc] initWithCapacity:1];
	}
	return self;
}


- (NSString*) name
{
	return [name copy];
}

- (void) setName:(NSString*)name_
{
	if (name)
		[name release];
	
	name = [name_ copy];
}

- (NSString*) teacher
{
	return [teacher copy];
}

- (void) setTeacher:(NSString*)teacher_
{
	if (teacher)
		[teacher release];
	
	teacher = [teacher_ copy];
}

- (NSString*) type
{
	return [type copy];
}

- (void) setType:(NSString*)type_
{
	if (type)
		[type release];
	
	type = [type_ copy];
}

- (NSArray*) marks
{
	return [marks copy];
}

- (void) addMark:(Mark*)mark
{
	[marks addObject:[mark retain]];
}

- (NSArray*)classMarks
{	
	return [self marksForType:@"classTest" andHalfyear:1];
}

- (NSArray*)otherMarks
{		
	return [self marksForType:@"test" andHalfyear:1];
}

- (NSArray*)marksForType:(NSString*)_type andHalfyear:(int)hyear
{
	NSMutableArray* mark= [[NSMutableArray alloc] initWithCapacity:1];
	NSArray* returnArray;
	
	int i, count = [marks count];
	for (i = 0; i < count; i++) {
		Mark* obj = [marks objectAtIndex:i];
		if ([[obj type] isEqualToString:_type] && ([obj halfYear] == hyear)) {
			[mark addObject:obj];
		}
	}
	
	returnArray = [[NSArray alloc] initWithArray:mark];
	[mark release];
	
	return returnArray;
}

- (NSArray*) lessons
{
	return [lessons copy];
}

- (void) addLesson:(Lesson*)lesson
{
	[lessons addObject:[lesson retain]];
}

- (float) average
{
	float average = 0.0f;
	int count = 0;
	float tmp = 0.0f;
	
	tmp = [self averageForHalfYear:0];
	if (tmp != 0) {
		average += tmp;
		count++;
	}
	
	tmp = [self averageForHalfYear:1];
	if (tmp != 0) {
		average += tmp;
		count++;
	}
	
	average = average/count;
	average = (float)((int)(average * 10)) / 10.0f;
	
	return average;
}

- (float) averageForHalfYear:(int)hyear
{
	float average = 0.0f;
	float average_other = 0.0f;
	float valency_other = 1.0f;
	int y = 0;
	
	int i, count = [marks count];
	for (i = 0; i < count; i++) {
		Mark* obj = [marks objectAtIndex:i];
		if ([obj halfYear] != hyear)
			continue;
		
		if ([[obj type] isEqualToString:@"classTest"]) {
			average += [obj mark]*[obj valency];
			valency_other -= [obj valency];
		}
		else {
			average_other += [obj mark];
			y++;
		}
	}
	
	average_other = average_other/y;
	average += average_other*valency_other;
	average = (float)((int)(average * 10)) / 10.0f;
	
	if (average < 1.0f || average > 6.0f) {
		return 0.0f;
	}
	return average;
}

- (NSString*)xml
{
	return [self xmlWithOffset:0];
}

- (NSString*)xmlWithOffset:(int)i
{
	int y;
	int count;
	NSMutableString* xml = [[NSMutableString alloc] initWithCapacity:50];
	NSMutableString* offset = [[NSMutableString alloc] initWithCapacity:i];
	
	for (y = 0; y < i; y++) {
		[offset appendString:@" "];
	}
	
	[xml appendFormat:@"%@<subject type=\"%@\">\n", offset, type];
	[xml appendFormat:@"%@    <name>%@</name>\n", offset, name];
	[xml appendFormat:@"%@    <teacher>%@</teacher>\n", offset, teacher];
	
	count = [marks count];
	for (y = 0; y < count; y++) {
		Subject* obj = [marks objectAtIndex:y];
		[xml appendString:[obj xmlWithOffset:i+4]];
	}
	
	count = [lessons count];
	for (y = 0; y < count; y++) {
		Lesson* obj = [lessons objectAtIndex:y];
		[xml appendString:[obj xmlWithOffset:i+4]];
	}
	[xml appendFormat:@"%@</subject>\n", offset];
	
	[offset release];

	return xml;
}

- (void) removeMark:(Mark*)mark
{
	int i, count = [marks count];
	for (i = 0; i < count; i++) {
		Mark * obj = [marks objectAtIndex:i];
		if (obj == mark) {
			[marks removeObjectAtIndex:i];
			count = [marks count];
		}
	}
}

- (void) dealloc
{
	[lessons release];
	[marks release];
	
	[super dealloc];
}


@end
